var picModal = {

}

var _this = '';
var total = '';
var searchText = '';

function addPictures(){
    $('#picModal').toggleClass('hidden');
    treePicInit();
    return _this = $(this);
}

function treePicInit(){
    var tree = $('#picTree');
    tree.jstree({ 'core': { data: null } });
    params.Method = "Q";
    $.post(
        mdmPath + "/MDM0019/MDM001902",
        params,
        function(re) {
            tree.jstree(true).settings.core.data = re.Content;
            tree.jstree(true).refresh();
        }
    );
}

(function treeNodeChange(){
    var tree = $('#picTree');
    tree.on("changed.jstree", function (e, data) {
        if (data.action === 'select_node') {
            params.Method = "Q";
            var Code = data.node.li_attr.Code, Name = data.node.li_attr.Name;
            params.Content = {
                "Folder": Code,
                "SearchText": searchText
            };
            params.page = 1;
            params.rows = 50;
            params.sidx = "Code";
            params.sord = "desc";
            //console.log(params);
            showPicAjax();
        }else{
            var Selected = $('li[code="ProductImages"]');
            Selected.find('>a').trigger('click');
        }
    });
})();

function showPicAjax(){
    var html = '';
    $.ajax({
        async: false,
        type: "post",   
        url: mdmPath + "/MDM0020/MDM002002", 
        data: params,
        dataType: "json",
        success: function(re){
            if('Content' in re){
                total = re.Content.total;
                var resImgs = re.Content.rows;
                for(var i=0,html='';i<resImgs.length;i++){
                    html += 
                    '<li class="u_add_item">'+
                        '<img src="'+resImgs[i].PicturePath+'" alt="">' +
                        // '<img src="'+resImgs[i].PicturePath1+'" alt="">' +
                        '<p class="u_desc">'+resImgs[i].Name+'</p>' +
                    '</li>'
                }
                //console.log(html);
                $('.u_modal_body:last').find('.u_pictures').html(html);
            }
            $(".u_modal_body .u_add_item").unbind("click").bind("click",function(){
                $(this).toggleClass("u_checked");
            });
        }
    });
    //console.log(html);
    return total;
}

(function turnPrev(){
    $("#prevBtn").on("click", function(){
        if(params.page>1){
            params.page--;
            showPicAjax();
        }
    });
})();

(function turnNext(){
    $("#nextBtn").on("click", function(){
        console.log(total);
        if(params.page<total){
            params.page++;
            showPicAjax();
        }
    }); 
})();

(function turnSearch(){
    $(".modal-data li[data-opt='opt_search']").find(">i").on("click", function(){
        searchText = $(".modal-data #opt_search").find(">input").val();
        params.Content.SearchText = searchText;
        showPicAjax();
    });
})();

(function addPicSubmit(){
    var count = 0;
    $("#addPicBtn").unbind("click").bind("click",function(){
        count = $('.u_content_wrapper .u_add_item img').length;
        //console.log(count)
        var imgs = $(".u_checked>img");
        var txts = $(".u_checked>p");
        if(imgs.length>0){
            for(var i=0,html='';i<imgs.length;i++){
                if(!_this.hasClass('u_add_item')){
                    html = '<img src="'+imgs[0].src+'" alt="">';
                    _this.html(html);
                    //console.log(PicName)
                    //console.log(txts.eq(0).html());
                    PictureName = txts.eq(0).html();
                }else{
                    for(var i=0;i<imgs.length;i++){
                        count ++;
                        count < 5 && (html += 
                        '<li class="u_add_item">'+
                            '<img src="'+imgs[i].src+'" alt="">'+
                            '<i class="fa fa-times fa-2x u_close"></i>'+
                            '<p class="u_desc">'+txts.eq(i).html()+'</p>'+
                        '</li>');
                        
                    }
                    _this.before(html);
                    $('.u_add_item .u_close').unbind('click').bind('click', function(){
                        $(this).parents().filter('.u_add_item').remove();
                        count > 4 &&(count = 4);
                        count --;
                    });
                }
            }
            $('#picModal').addClass('hidden');
        }
    });
})();
